package com.hangwei.paperless.view.window;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangwei.paperless.MainSeeActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.adapter.SubProcessAdapter;
import com.hangwei.paperless.model.bean.SubProcessInfo;
import com.hangwei.paperless.model.bean.SubRepairList;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.presenter.SubProcessPresenter;
import com.hangwei.paperless.view.BaseView;
import com.hangwei.paperless.view.SealHelper;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.Tools;

/**
 * Created by 张龙臣 on 2020/11/24.
 */

public class SubProcessSee extends BaseView<MainSeeActivity, SubProcessInfo> {

    private EditText mEtRemark;
    private SubProcessAdapter mAdapter;
    private RelativeLayout mFlUserSeal;

    public SubProcessSee(MainSeeActivity activity) {
        super(activity);
    }

    @Override
    protected View initView() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.page_see_sub_process, null);
//        ImageView ivInspectorSeal = (ImageView) inflate.findViewById(R.id.ivInspectorSeal);
        TextView tvEffective = (TextView) inflate.findViewById(R.id.tvEffective);
        inflate.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        SwipeRefreshLayout refreshLayout = inflate.findViewById(R.id.refreshLayout);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new SubProcessAdapter(null) {
            @Override
            protected void savePn(String id, String pn, final CallBack<Object> callBack) {
                final LoadingDialog dialog = new LoadingDialog(mActivity);
                dialog.show();
                String userId = SPUtil.getString(SPKey.Account.USER_ID);
                StaticPresenter.saveRoutingSheetListInfo(userId, mActivity.getBillId(), id, pn, new CallBack<Object>() {
                    @Override
                    public void onCompleted(Object o) {
                        dialog.dismiss();
                        callBack.onCompleted(null);
                    }

                    @Override
                    public void onError() {
                        dialog.dismiss();
                        callBack.onError();
                    }
                });
            }
        };
        mAdapter.setEnable(false);
        SubProcessPresenter subProcessPresenter = new SubProcessPresenter(refreshLayout, mAdapter, recyclerView);
        subProcessPresenter.setBillId(mActivity.getBillId());
        mAdapter.addFooterView(getFooter());
        mAdapter.setEnableLoadMore(false);
        recyclerView.setAdapter(mAdapter);
        LoadingDialog loadingDialog = new LoadingDialog(mActivity);
        loadingDialog.show();
        subProcessPresenter.refresh(new CallBack<SubRepairList>() {
            @Override
            public void onCompleted(SubRepairList subRepairList) {
                loadingDialog.dismiss();
                mAdapter.setEnableLoadMore(true);
                if (subRepairList!=null){
                    mEtRemark.setText(Tools.checkedString(subRepairList.remark));
                    SealHelper sealHelper = new SealHelper(mActivity) {
                        @Override
                        protected void onSeal() {

                        }
                    };
                    sealHelper.bindData(new SealHelper.SealBean(true, subRepairList.inspectorStampId, subRepairList.inspectorId, subRepairList.inspectorStampTypeCode, subRepairList.inspectorStampCode, subRepairList.inspectorDate, subRepairList.inspector, true));
                    mFlUserSeal.removeAllViews();
                    mFlUserSeal.addView(sealHelper.getItemView());
                }
            }

            @Override
            public void onError() {
                loadingDialog.dismiss();
            }
        });
//        initData();
        return inflate;
    }

    private View getFooter() {
       View view =  LayoutInflater.from(mActivity).inflate(R.layout.footer_sub_process,null);
        mEtRemark = (EditText) view.findViewById(R.id.etRemark);
        mEtRemark.setEnabled(false);
        mFlUserSeal = view.findViewById(R.id.flUserSeal);
        return view;
    }

    @Override
    public void bindData(SubProcessInfo subProcessInfo) {
        mEtRemark.setText(Tools.checkedString(subProcessInfo.remark));
        SealHelper sealHelper =  new SealHelper(mActivity) {
            @Override
            protected void onSeal() {

            }
        };
        sealHelper.bindData(new SealHelper.SealBean(true,subProcessInfo.inspectorStampId,subProcessInfo.inspectorId,subProcessInfo.inspectorStampTypeCode,subProcessInfo.inspectorStampCode,subProcessInfo.inspectorDate,subProcessInfo.inspector,false));
        mFlUserSeal.removeAllViews();
        mFlUserSeal.addView(sealHelper.getItemView());
        mAdapter.setNewData(subProcessInfo.recordList);
//        mAdapter.setData();
    }

    private void initData() {
        final LoadingDialog dialog = new LoadingDialog(mActivity);
        dialog.show();
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getRoutingSheetList(userId, mActivity.getBillId(), new CallBack<SubProcessInfo>() {
            @Override
            public void onCompleted(SubProcessInfo subProcessInfo) {
                bindData(subProcessInfo);
                dialog.dismiss();
            }

            @Override
            public void onError() {
                dialog.dismiss();
            }
        });
    }
}
